#include "processpool.hpp"

int main()
{
    // 1.初始化进程池，父进程拿到channels
    ProcessPool pp;
    pp.InitProcessPool([](int fd)
                    {
         while(true)
         {
             int x=0;
             //cout << "子进程要休眠了: " << getpid() << endl;
             ssize_t n = read(fd,&x,sizeof(x));
             if(n > 0)
             {
                 cout << "子进程被唤醒了: " << getpid() <<endl;
                 if(x >= 0 && x <= tasks.size())
                 {
                    //执行相应的任务
                    tasks[x]();
                 }
                 else
                 {
                    cout<<"父进程传的是一个错误的任务码！"<<endl;
                 }
             }
             else if(n == 0)
             {
                cout << "子进程要结束了: "<< getpid() <<endl;
                break;
             }
         } });
    
    pp.CtrlProcessChild(10);

    pp.WaitProcessChild();
    cout << "父进程控制子进程结束了,整个过程结束了" << endl;
    return 0;
}